/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.view.bar;

import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import simplemetrosystem.model.charlanguage.NameMap;
import simplemetrosystem.view.MetroFrame;
import simplemetrosystem.view.action.ClearActionListener;
import simplemetrosystem.view.action.NewEdgeActionListener;
import simplemetrosystem.view.action.NewStationActionListener;
import simplemetrosystem.view.action.NewTextActionListener;
import simplemetrosystem.view.action.SelectActionListener;

/**
 *
 * @author Eric Qian
 */
public class EditToolBar extends JToolBar {

    /**
     * 
     * @param frame
     */
    public EditToolBar(MetroFrame frame) {
        super();
        this.frame = frame;
        nameMap = NameMap.getInstance();
        name = nameMap.getName("Edit");
        this.setName(name);
        this.setEnabled(true);
        this.setFloatable(false);
        this.setOrientation(JToolBar.VERTICAL);
        this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        model = false;
        addItem();

    }

    /**
     * 
     */
    private void addItem() {
        group = new ButtonGroup();

        this.add(getSelectButton());
        group.add(getSelectButton());
        this.add(getTextButton());
        group.add(getTextButton());
        this.add(getNewStationButton());
        group.add(getNewStationButton());
        this.add(getNewEdgeButton());
        group.add(getNewEdgeButton());
        this.add(getClearButton());
        group.add(getClearButton());

        getSelectButton().setSelected(true);
    }

    /**
     * 
     * @return
     */
    public JToggleButton getSelectButton() {
        if (selectButton == null) {
            selectButton = new JToggleButton();
            Icon icon = new ImageIcon("icon/select.gif");
            selectButton.setIcon(icon);
            //selectButton.setText(nameMap.getName("Select"));
            selectButton.setToolTipText(nameMap.getName("Select"));
            selectButton.addActionListener(new SelectActionListener(frame));
        }
        return selectButton;
    }

    /**
     * 
     * @return
     */
    public JToggleButton getTextButton() {
        if (textButton == null) {
            textButton = new JToggleButton();
            Icon icon = new ImageIcon("icon/text.gif");
            textButton.setIcon(icon);
            //textButton.setText(nameMap.getName("Text"));
            textButton.setToolTipText(nameMap.getName("Text"));
            textButton.addActionListener(new NewTextActionListener(frame));
        }
        return textButton;
    }

    /**
     * 
     * @return
     */
    public JToggleButton getNewStationButton() {
        if (newStationButton == null) {
            newStationButton = new JToggleButton();
            Icon icon = new ImageIcon("icon/station.gif");
            newStationButton.setIcon(icon);
            //newStationButton.setText(nameMap.getName("New Station"));
            newStationButton.setToolTipText(nameMap.getName("New Station"));
            newStationButton.addActionListener(new NewStationActionListener(frame));
        }
        return newStationButton;
    }

    /**
     * 
     * @return
     */
    public JToggleButton getNewEdgeButton() {
        if (newEdgeButton == null) {
            newEdgeButton = new JToggleButton();
            Icon icon = new ImageIcon("icon/edge.gif");
            newEdgeButton.setIcon(icon);
            //newEdgeButton.setText(nameMap.getName("New Edge"));
            newEdgeButton.setToolTipText(nameMap.getName("New Edge"));
            newEdgeButton.addActionListener(new NewEdgeActionListener(frame));
        }
        return newEdgeButton;
    }

    /**
     * 
     * @return
     */
    public JToggleButton getClearButton() {
        if (clearButton == null) {
            clearButton = new JToggleButton();
            Icon icon = new ImageIcon("icon/clear.gif");
            clearButton.setIcon(icon);
            //clearButton.setText(nameMap.getName("Clear"));
            clearButton.setToolTipText(nameMap.getName("Clear"));
            clearButton.addActionListener(new ClearActionListener(frame));
        }
        return clearButton;
    }

    public void setModel(boolean model) {
        if (this.model != model) {
            this.model = model;
            if (model) {
                selectButton.setEnabled(false);
                textButton.setEnabled(false);
                newStationButton.setEnabled(false);
                newEdgeButton.setEnabled(false);
                clearButton.setEnabled(false);
            } else {
                selectButton.setEnabled(true);
                textButton.setEnabled(true);
                newStationButton.setEnabled(true);
                newEdgeButton.setEnabled(true);
                clearButton.setEnabled(true);
            }
        }
    }
    private String name;
    private NameMap nameMap;
    private MetroFrame frame;
    private ButtonGroup group;
    private JToggleButton selectButton;
    private JToggleButton textButton;
    private JToggleButton newStationButton;
    private JToggleButton newEdgeButton;
    private JToggleButton clearButton;
    private boolean model;
}
